﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.InvTransferServiceRef;
    using WMS6.Web.UserControls;

    public class OwnerTransFerDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected DropDownList ddlFromUomID;
        protected HtmlForm F5068;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HiddenField hdnExpiryDate;
        protected HiddenField hdnExternal;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnFromOwnerID;
        protected HtmlInputHidden hdnFromSkuID;
        protected HiddenField hdnLot1;
        protected HiddenField hdnLot10;
        protected HiddenField hdnLot2;
        protected HiddenField hdnLot3;
        protected HiddenField hdnLot4;
        protected HiddenField hdnLot5;
        protected HiddenField hdnLot6;
        protected HiddenField hdnLot7;
        protected HiddenField hdnLot8;
        protected HiddenField hdnLot9;
        protected HtmlInputHidden hdnOriginQty;
        protected HiddenField hdnProductDate;
        protected HtmlInputHidden hdnToOriginQty;
        protected HtmlInputHidden hdnToOwnerID;
        protected HtmlInputHidden hdnToSkuID;
        protected HtmlInputHidden hdnTransferID;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected Label lblFromPackID;
        protected Label lblFromSkuDescr;
        protected Label lblFromSkuID;
        protected Label lblFromUomID;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblStatus;
        protected Label lblToQty;
        protected Label lblToSkuDescr;
        protected Label lblToSkuID;
        protected Label lblTransferDetail;
        protected LotAtrributeCtrl LotAtrributeCtrl1;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtFromPackID;
        protected TextBox txtFromSkuDescr;
        protected TextBox txtFromSkuID;
        protected TextBox txtLineID;
        protected TextBox txtStatus;
        protected TextBox txtToQty;
        protected TextBox txtToSkuDescr;
        protected TextBox txtToSkuID;

        private void BindData2UomList(string packID)
        {
            try
            {
                List<WMS6.Web.CommonMethodServiceRef.UomList> uomList = CommoneMethodServiceClientProxy.GetInstance().GetUomListByPackID(packID);
                if ((uomList != null) && (uomList.Count > 0))
                {
                    base.BindUomList2DDL(this.ddlFromUomID, uomList);
                    this.ViewState["UOMLIST"] = uomList;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_030") + ex.Message;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("OwnerTransFer.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnTransferID.Value.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string temp;
                if (this.txtStatus.Text.ToUpper() != "NEW")
                {
                    base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F5300_013"));
                    return;
                }
                if (string.IsNullOrEmpty(this.txtToSkuID.Text) || string.IsNullOrEmpty(this.txtFromSkuID.Text))
                {
                    base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F5300_021"));
                    return;
                }
                if (string.IsNullOrEmpty(this.txtToQty.Text.Trim()))
                {
                    base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F5300_015"));
                    return;
                }
                if (decimal.Parse(this.txtToQty.Text) <= 0M)
                {
                    base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F5300_015"));
                    return;
                }
                if (string.IsNullOrEmpty(this.txtFromSkuID.Text.Trim()))
                {
                    base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F5300_018"));
                    return;
                }
                OwnerTransferDetailInfo transferDetailInfo = new OwnerTransferDetailInfo {
                    TransferID = this.hdnTransferID.Value,
                    LineID = int.Parse(this.txtLineID.Text),
                    FromOwnerID = this.hdnFromOwnerID.Value,
                    FromSkuID = this.txtFromSkuID.Text.Trim(),
                    ToOwnerID = this.hdnToOwnerID.Value,
                    ToSkuID = this.txtToSkuID.Text.Trim(),
                    FromPackID = this.txtFromPackID.Text.Trim(),
                    FromUomID = this.ddlFromUomID.SelectedValue,
                    ToQty = decimal.Parse(this.txtToQty.Text.Trim()),
                    Status = this.txtStatus.Text,
                    ToPackID = this.txtFromPackID.Text.Trim(),
                    ToUomID = this.ddlFromUomID.SelectedValue,
                    FromLoc = "",
                    ToLoc = "",
                    ToLot = "",
                    FromLot = "",
                    FromLpnID = "",
                    ToLpnID = "",
                    FromQty = 0M
                };
                ReceiptLotInfo transferLot = new ReceiptLotInfo {
                    VendorID = "",
                    LotAttr01 = "",
                    LotAttr02 = "",
                    LotAttr03 = "",
                    LotAttr04 = "",
                    LotAttr05 = "",
                    LotAttr06 = "",
                    LotAttr07 = "",
                    LotAttr08 = "",
                    LotAttr09 = "",
                    LotAttr10 = ""
                };
                List<Control> lotControls = this.Session["LotAttributeControls"] as List<Control>;
                this.Session["LotAttributeControls"] = null;
                if (lotControls != null)
                {
                    foreach (Control ctrl in lotControls)
                    {
                        DateTime CS00002;
                        object tempObj = base.Request.Form[this.LotAtrributeCtrl1.ClientID.ToString() + "$" + ctrl.ID.ToString()];
                        temp = "";
                        if (tempObj != null)
                        {
                            temp = tempObj.ToString();
                        }
                        if (ctrl.ID == "txtProduct")
                        {
                            if (!string.IsNullOrEmpty(temp))
                            {
                                transferLot.ProduceDate = DateTime.Parse(temp);
                            }
                            else
                            {
                                CS00002 = new DateTime();
                                transferLot.ProduceDate = CS00002;
                            }
                        }
                        else if (ctrl.ID == "txtExpiryDate")
                        {
                            if (!string.IsNullOrEmpty(temp))
                            {
                                transferLot.ExpiryDate = DateTime.Parse(temp);
                            }
                            else
                            {
                                CS00002 = new DateTime();
                                transferLot.ExpiryDate = CS00002;
                            }
                        }
                        else if (ctrl.ID == "txtExternalLot")
                        {
                            if (!string.IsNullOrEmpty(temp))
                            {
                                transferLot.ExternalLot = temp;
                            }
                            else
                            {
                                transferLot.ExternalLot = "";
                            }
                        }
                        else
                        {
                            switch (ctrl.ID.Substring(4))
                            {
                                case "1":
                                    transferLot.LotAttr01 = temp;
                                    break;

                                case "2":
                                    transferLot.LotAttr02 = temp;
                                    break;

                                case "3":
                                    transferLot.LotAttr03 = temp;
                                    break;

                                case "4":
                                    transferLot.LotAttr04 = temp;
                                    break;

                                case "5":
                                    transferLot.LotAttr05 = temp;
                                    break;

                                case "6":
                                    transferLot.LotAttr06 = temp;
                                    break;

                                case "7":
                                    transferLot.LotAttr07 = temp;
                                    break;

                                case "8":
                                    transferLot.LotAttr08 = temp;
                                    break;

                                case "9":
                                    transferLot.LotAttr09 = temp;
                                    break;

                                case "10":
                                    transferLot.LotAttr10 = temp;
                                    break;
                            }
                        }
                    }
                }
                transferDetailInfo.TransferLotInfo = transferLot;
                transferDetailInfo.TransferDate = DateTime.Now;
                transferDetailInfo.CreatedBy = base.UserInfo.UserID;
                transferDetailInfo.UpdatedBy = base.UserInfo.UserID;
                if ((this.hdnType.Value != "") && (this.hdnType.Value.ToUpper() == "EDIT"))
                {
                    temp = InvTransferServiceClientProxy.GetInstance().UpdateOwnerTransferDetail(transferDetailInfo);
                    this.lblMsg.Text = temp + base.GetGlobalMsgResource("C002");
                }
                else
                {
                    temp = InvTransferServiceClientProxy.GetInstance().InsertOwnerTransferDetailInfo(transferDetailInfo);
                    this.lblMsg.Text = temp + base.GetGlobalMsgResource("C004");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_019") + ex.Message;
                return;
            }
            base.Response.Redirect(string.Format("OwnerTransFer.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "2", base.UrlEncode(this.hdnTransferID.Value.Trim())));
        }

        private void LoadData()
        {
            this.hdnType.Value = "new";
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnTransferID.Value = base.Request.QueryString["ID"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"]))
            {
                this.txtLineID.Text = base.Request.QueryString["SeqNO"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["FOwnerID"]))
            {
                this.hdnFromOwnerID.Value = base.Request.QueryString["FOwnerID"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["TOwnerID"]))
            {
                this.hdnToOwnerID.Value = base.Request.QueryString["TOwnerID"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetTransferDetail2From();
            }
            else
            {
                this.txtStatus.Text = "New";
            }
            if (this.txtStatus.Text.ToUpper() == "CONFIRMED")
            {
                this.btnSave.Enabled = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.LotAtrributeCtrl1.LoadData(this.txtFromSkuID.Text, this.hdnFromOwnerID.Value, this.LotAtrributeCtrl1.ClientID, false, 3);
            this.lblMsg.Text = "";
        }

        private void SetTransferDetail2From()
        {
            try
            {
                OwnerTransferDetailInfo transferDetail = InvTransferServiceClientProxy.GetInstance().GetOwnerTransferDetailInfo(this.hdnTransferID.Value, this.txtLineID.Text);
                if (transferDetail != null)
                {
                    this.txtStatus.Text = transferDetail.Status;
                    this.txtFromSkuID.Text = transferDetail.FromSkuID;
                    this.txtToSkuID.Text = transferDetail.ToSkuID;
                    this.txtFromSkuDescr.Text = transferDetail.FromSkuDescr;
                    this.txtToSkuDescr.Text = transferDetail.ToSkuDescr;
                    this.txtFromPackID.Text = transferDetail.FromPackID;
                    if (!string.IsNullOrEmpty(this.txtFromPackID.Text.Trim()))
                    {
                        base.BindUomList2DDL(this.ddlFromUomID, this.txtFromPackID.Text.Trim());
                        this.ddlFromUomID.SelectedValue = transferDetail.FromUomID;
                    }
                    this.txtToQty.Text = base.GetPreferenceDigit(transferDetail.ToQty);
                    if (transferDetail.TransferLotInfo != null)
                    {
                        this.hdnLot1.Value = transferDetail.TransferLotInfo.LotAttr01;
                        this.hdnLot2.Value = transferDetail.TransferLotInfo.LotAttr02;
                        this.hdnLot3.Value = transferDetail.TransferLotInfo.LotAttr03;
                        this.hdnLot4.Value = transferDetail.TransferLotInfo.LotAttr04;
                        this.hdnLot5.Value = transferDetail.TransferLotInfo.LotAttr05;
                        this.hdnLot6.Value = transferDetail.TransferLotInfo.LotAttr06;
                        this.hdnLot7.Value = transferDetail.TransferLotInfo.LotAttr07;
                        this.hdnLot8.Value = transferDetail.TransferLotInfo.LotAttr08;
                        this.hdnLot9.Value = transferDetail.TransferLotInfo.LotAttr09;
                        this.hdnLot10.Value = transferDetail.TransferLotInfo.LotAttr10;
                        this.hdnProductDate.Value = base.GetPreferenceShortDate(transferDetail.TransferLotInfo.ProduceDate);
                        this.hdnExpiryDate.Value = base.GetPreferenceShortDate(transferDetail.TransferLotInfo.ExpiryDate);
                        this.hdnExternal.Value = transferDetail.TransferLotInfo.ExternalLot;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void txtFromPackID_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                base.BindUomList2DDL(this.ddlFromUomID, this.txtFromPackID.Text.Trim());
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_020") + ex.Message;
                this.txtFromPackID.Focus();
            }
        }

        protected void txtFromSkuID_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string skuID = this.txtFromSkuID.Text.Trim();
                if (string.IsNullOrEmpty(skuID))
                {
                    this.txtFromSkuDescr.Text = "";
                    this.txtFromPackID.Text = "";
                    this.ddlFromUomID.Items.Clear();
                }
                else
                {
                    SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnFromOwnerID.Value, skuID, "", "SKU") as SkuInfo;
                    if (!(((skuInfo == null) || string.IsNullOrEmpty(skuInfo.SkuID)) || string.IsNullOrEmpty(skuInfo.OwnerID)))
                    {
                        this.txtFromSkuDescr.Text = skuInfo.Descr;
                        this.txtFromPackID.Text = skuInfo.PackID;
                        this.BindData2UomList(this.txtFromPackID.Text.Trim());
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4100_031");
                        this.txtFromSkuID.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.txtFromSkuID.Focus();
            }
        }

        protected void txtToSkuID_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string skuID = this.txtToSkuID.Text.Trim();
                if (string.IsNullOrEmpty(skuID))
                {
                    this.txtToSkuDescr.Text = "";
                }
                else
                {
                    SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnToOwnerID.Value, skuID, "", "SKU") as SkuInfo;
                    if (!(((skuInfo == null) || string.IsNullOrEmpty(skuInfo.SkuID)) || string.IsNullOrEmpty(skuInfo.OwnerID)))
                    {
                        this.txtToSkuDescr.Text = skuInfo.Descr;
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4100_031");
                        this.txtToSkuID.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.txtToSkuID.Focus();
            }
        }
    }
}

